package com.example.stock.controller;

import com.example.stock.controller.dto.IndustryPanoramaDTO;
import com.example.stock.controller.dto.MonthPctStatDTO;
import com.example.stock.excel.Wugu;
import com.example.stock.model.DailyDetail;
import com.example.stock.model.WeekDetail;
import com.example.stock.service.*;
import com.example.stock.util.Result;
import com.example.stock.util.ResultGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


@CrossOrigin(origins = "*")
@RequestMapping("/industry")
@RestController
public class IndustryController {

    @Autowired
    MonthDataService monthDataService;

    @Autowired
    WeekDataService weekDataService;

    @Autowired
    YearDataService yearDataService;

    @Autowired
    WuguService wuguService;

    @Autowired
    IndustryDataService industryDataService;

    @Autowired
    DailyDataService dailyDataService;

    @GetMapping()
    public Result index() {
        return ResultGenerator.genSuccessResult(industryDataService.getTagsMap());
    }

    @GetMapping("{industry}")
    public Result industry(@PathVariable("industry") String industry) {
        return ResultGenerator.genSuccessResult(industryDataService.getStocksByTag(industry));
    }

    // 行业全景图
    @GetMapping("panorama")
    public Result panorama() {

        String first = monthDataService.getMonthPctStatKeys().getFirst();

        List<MonthPctStatDTO> stocks = monthDataService.getMonthPctStatByDate(first).stream()
                .filter(item -> item.getCategory().equals("股票"))
                .toList();

        List<IndustryPanoramaDTO> ret = new ArrayList<>();

        stocks.forEach(item -> {

            IndustryPanoramaDTO industryPanoramaDTO = new IndustryPanoramaDTO();
            BeanUtils.copyProperties(item, industryPanoramaDTO);

            String stock = item.getStock();

            // 使用最新吾股数据
            Wugu rawWugu = wuguService.getRawWugu(stock);
            if (rawWugu != null) {
                industryPanoramaDTO.setWg(rawWugu.getWugu());
                industryPanoramaDTO.setIndustry(rawWugu.getIndustry());
            }

            // 日
            DailyDetail lastTradingRankByStock = dailyDataService.getLastedDailyDetailByStock(stock);
            industryPanoramaDTO.setDayPct(lastTradingRankByStock.getPct());

            // 周
            WeekDetail currentWeekDetail = weekDataService.getCurrentWeekDetail(stock);
            industryPanoramaDTO.setWeekPct(currentWeekDetail.getWeekPct());

            // 年
            industryPanoramaDTO.setYearPct(yearDataService.getLastYearPct(stock));

            ret.add(industryPanoramaDTO);
        });

        Map<String, List<IndustryPanoramaDTO>> collect = ret.stream().collect(Collectors.groupingBy(IndustryPanoramaDTO::getIndustry,
            Collectors.collectingAndThen(Collectors.toList(),
                // 按 分位点 排序
                list -> list.stream().sorted(Comparator.comparing(IndustryPanoramaDTO::getLp)).toList()
            )
        ));

        return ResultGenerator.genSuccessResult(collect);
    }
}
